﻿using System.Collections.Generic;
using MSP.Data.Domain;
using MSP.Data.NHibernate;
using MSP.Data.Repositories;
using MSP.Business.Logic;
using MSP.Data.Domain.Enums;

namespace MSP.Business.ProductManagement
{
    public class ProductManager
    {
        public int SaveProduct(Product product)
        {
            int productId = 0;

            if (product!= null)
            {
                using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
                {
                    var productRepository = new ProductRepository(unitOfWork.Session);
                    productRepository.Save(product);
                    unitOfWork.Commit();
                }

                productId = product.Id;
            }

            return productId;
        }

        public int SaveOrUpdate(Product product)
        {
            int productId = 0;

            if (product != null)
            {
                using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
                {
                    var productRepository = new ProductRepository(unitOfWork.Session);
                    productRepository.SaveOrUpdateCopy(product);
                    unitOfWork.Commit();
                }

                productId = product.Id;
            }

            return productId;
        }

        public bool UpdateProduct(Product product)
        {
            bool result = false;

            using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
            {
                var productRepository = new ProductRepository(unitOfWork.Session);
                result = productRepository.Update(product);
                unitOfWork.Commit();
            }

            return result;
        }

        public bool DeleteProduct(Product product)
        {
            bool result = false;
            using (var unitOfWork = new UnitOfWork(NHibernateSessionFactory.SessionFactory))
            {
                var productRepository = new ProductRepository(unitOfWork.Session);
                result = productRepository.Delete(product);
                unitOfWork.Commit();
            }

            return result;
        }

        public Product GetProductById(int Id)
        {
            var productRepository = new ProductRepository(NHibernateSessionFactory.SessionFactory.OpenSession());
            Product product = productRepository.GetProductById(Id);
            return product;
        }

        public Product GetProductByBarcode(string Barcode)
        {
            var productRepository = new ProductRepository(NHibernateSessionFactory.SessionFactory.OpenSession());
            Product product = productRepository.GetProductByBarcode(Barcode);
            return product;
        }

        public IList<Product> GetAllProduct()
        {
            var productRepository = new ProductRepository(NHibernateSessionFactory.SessionFactory.OpenSession());
            IList<Product> products = productRepository.GetAllProduct();

            return products;
        }

        public IList<KeyValuePair> GetAllUnit()
        {
            return KeyValuePair.ListFrom<UnitTypeEnum>();
        }

    }
}
